package com.wise.aip.service;

import org.apache.commons.lang.StringUtils;

import com.wise.aip.auth.GatewayAuth;
import com.wise.aip.template.ParamTemplate;
import com.wise.api.json.utils.ApiJsonUtils;

public class BaseDataService implements Action {

    @Override
    public String save(String requestURL, String param) throws Exception {
        return requestPostJson(requestURL, param);
    }

    @Override
    public String find(String requestURL, String param) throws Exception {
        return requestPostJson(requestURL, param);
    }

    @Override
    public String update(String requestURL, String param) throws Exception {
        return requestPostJson(requestURL, param);
    }

    @Override
    public String delete(String requestURL, String param) throws Exception {
        return requestPostJson(requestURL, param);
    }

    @Override
    public String forbid(String requestURL, String param) throws Exception {
        return requestPostJson(requestURL, param);
    }

    /**
     * 
     * <p>
     * Title: deleteOrforbid
     * </p>
     * <p>
     * Description: 删除失败时调禁用接口
     * </p>
     * 
     * @param requestURL 请求URL
     * @param number 基础资料编码
     * @return
     * @throws Exception
     */
    protected String deleteOrforbid(String requestURL, String number) throws Exception {

        String res = this.delete(requestURL, ParamTemplate.delete(number));
        if (!ApiJsonUtils.checkStatus(res)) {
            res = this.forbid(requestURL, ParamTemplate.forbid(number));
        }

        return res;

    }

    private String requestPostJson(String url, String param) throws Exception {

        if (StringUtils.isEmpty(url) || StringUtils.isEmpty(param)) {
            return null;
        }

        return GatewayAuth.gatewayPostJson(url, param);
    }

}
